package ext.generic.doc.processors;
import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmClipboardItem;
import com.ptc.netmarkets.util.beans.NmClipboardUtility;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmActionServiceHelper;
import com.ptc.netmarkets.util.misc.NmContext;
import com.ptc.netmarkets.util.misc.NmContextItem;
import com.ptc.windchill.enterprise.util.PartManagementHelper;

import ext.generic.part.GenericPartManagementHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Stack;
import java.util.Vector;
import org.apache.log4j.Logger;
import wt.doc.WTDocument;
import wt.fc.Persistable;
import wt.fc.ReferenceFactory;
import wt.fc.WTReference;
import wt.fc.collections.WTArrayList;
import wt.fc.collections.WTCollection;
import wt.httpgw.URLFactory;
import wt.log4j.LogR;
import wt.part.PartDocHelper;
import wt.part.WTPart;
import wt.part.WTProductInstance2;
import wt.session.SessionHelper;
import wt.util.LocalizableMessage;
import wt.util.WTException;
import wt.util.WTMessage;

public class GenericDocSelectPartDescPasteRelationFormProcessorr extends DefaultObjectFormProcessor
{
  private static final String RESOURCE = "com.ptc.windchill.enterprise.part.partResource";
  private static final Logger log;
  protected String redirectURL = null;

  public FormResult doOperation(NmCommandBean paramNmCommandBean, List<ObjectBean> paramList)
    throws WTException
  {
    WTDocument localWTDocument = (WTDocument)paramNmCommandBean.getPrimaryOid().getWtRef().getObject();
    Vector localVector = new Vector();
    log.debug("ENTER : PartDocDescPasteRelation.paste");

    boolean bool = PartManagementHelper.getWcPDMMethodPref();
    WTArrayList localWTArrayList = new WTArrayList();
    if ((bool) || (!PartDocHelper.isReferenceDocument(localWTDocument))) {
      localWTArrayList.add(localWTDocument);
    }

    NmContext localNmContext = NmContext.fromString(paramNmCommandBean.getCompContext());
    Stack localStack = localNmContext.getContextItems();
    int i = localStack.size();
    int j = 0;
    Object localObject1;
    for (int k = 0; k < i; k++) {
      localObject1 = (NmContextItem)localStack.get(k);
      if (((NmContextItem)localObject1).getAction().equalsIgnoreCase("relatedDocumentPartInstances")) {
        j = 1;
      }
    }

    FormResult localFormResult = new FormResult();
    localFormResult.setStatus(FormProcessingStatus.SUCCESS);

    if (localWTArrayList.size() > 0) {
      localObject1 = NmClipboardUtility.getClipboardItemsToPaste(paramNmCommandBean);

      if (PartManagementHelper.isClipboardEmpty((List)localObject1, paramNmCommandBean, localFormResult)) {
        return localFormResult;
      }

      for (int m = 0; m < ((List)localObject1).size(); m++) {
        NmClipboardItem localNmClipboardItem = (NmClipboardItem)((List)localObject1).get(m);
        NmOid localNmOid = NmClipboardUtility.getNmOid(localNmClipboardItem);
        log.debug("oid and type - " + localNmOid);

        if (localNmOid != null) {
          Persistable localPersistable = localNmOid.getWtRef().getObject();
          if (PartManagementHelper.isSameContainer(localPersistable, localWTDocument))
          {
            Object localObject2;
            if (((localPersistable instanceof WTPart)) && (j == 0)) {
              localObject2 = (WTPart)localPersistable;
              Object localObject3;
              if (PartManagementHelper.isNewInWorkspace(localObject2)) {
                localObject3 = SessionHelper.getLocale();
                Object[] arrayOfObject = null;
                ArrayList localArrayList = new ArrayList();
                String str = ((WTPart)localObject2).getName();
                localArrayList.add(str);
                arrayOfObject = localArrayList.toArray();
                WTMessage localWTMessage = new WTMessage("com.ptc.windchill.enterprise.part.partResource", "109", arrayOfObject);
                FeedbackMessage localFeedbackMessage = new FeedbackMessage(FeedbackType.FAILURE, (Locale)localObject3, null, null, new LocalizableMessage[] { localWTMessage });
                localFormResult.addFeedbackMessage(localFeedbackMessage);
                localFormResult.setStatus(FormProcessingStatus.FAILURE);
                return localFormResult;
              }
              try
              {
                localObject3 = GenericPartManagementHelper.createRelationship(localWTArrayList, (WTPart)localObject2, false, true, false);
                if (((WTCollection)localObject3).size() > 0) {
                  localVector.add(localPersistable);
                }

              }
              catch (WTException localWTException)
              {
                localVector.add(localObject2);
              }
            }
            else if (((localPersistable instanceof WTProductInstance2)) && (j != 0)) {
              localObject2 = (WTProductInstance2)localPersistable;

              WTCollection localWTCollection = GenericPartManagementHelper.createRelationship(localWTArrayList, (WTProductInstance2)localObject2, false);
              if (localWTCollection.size() == 2)
                localVector.add(localPersistable);
            }
            else
            {
              localVector.add(localPersistable);
            }
          }
          else {
            localVector.add(localPersistable);
          }
        }
      }

      if (localVector.size() != 0)
      {
        Locale localLocale = SessionHelper.getLocale();
        localFormResult.addFeedbackMessage(PartManagementHelper.getPasteFbMsg(FeedbackType.FAILURE, localVector, localLocale));
        localFormResult.setStatus(FormProcessingStatus.SUCCESS);
      }
    }
    log.debug("EXIT : PartDocDescPasteRelation.paste)");

    super.doOperation(paramNmCommandBean, paramList);
    return (FormResult)(FormResult)(FormResult)localFormResult;
  }
  protected String getURL(Object obj) throws WTException
  {
      ReferenceFactory referencefactory = new ReferenceFactory();
      String s = referencefactory.getReferenceString((Persistable) obj);
      URLFactory urlfactory = new URLFactory();
      HashMap hashmap = new HashMap();
      hashmap.put("oid", s);
      if (obj instanceof WTPart)
      {
          hashmap.put("tlnType", "part");
          hashmap.put("tlnAction", "relatedPartsDocuments");
      }
      else
      {
          hashmap.put("tlnType", "instance");
          hashmap.put("tlnAction", "relatedPartInstanceDocuments");
      }
      String s1 = NmActionServiceHelper.service.getAction("object", "view").getUrl();
      redirectURL = urlfactory.getHREF(s1, hashmap, true);
      return redirectURL;
  }
  static
  {
    try
    {
      log = LogR.getLogger(GenericDocSelectPartDescPasteRelationFormProcessorr.class.getName());
    }
    catch (Exception localException) {
      throw new ExceptionInInitializerError(localException);
    }
  }
}
